#include <cstring>
#include <iostream>
#include<algorithm>

using namespace std;

const int N = 110, M = 220;

int n, m;
int h[N], e[M], w[M], ne[M], idx;
int f[N][N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs(int u, int father) {
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == father) continue;
        dfs(j, u);
        for (int k = m; k; -- k)
            for (int t = 0; t < k; ++ t)
                f[u][k] = max(f[u][k], f[u][k - t - 1] + f[j][t] + w[i]);
    }
} 

int main() {
    cin >> n >> m;
    
    int t = n - 1;
    memset(h, -1, sizeof h);
    while (t -- ) {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    
    dfs(1, -1);
    
    cout << f[1][m] << endl;
    
    return 0;
}